package com.dhcc.core.modules.system.controller;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.config.ConfigCache;
import com.dhcc.core.modules.system.cache.config.IConfigCache;
import com.dhcc.core.modules.system.entity.Config;
import com.dhcc.core.modules.system.service.IConfigService;
import com.dhcc.core.modules.system.warpper.ConfigWarpper;


/**
 * 参数配置表
 * 
 * @ClassName: ConfigController
 * @Description: TODO
 * @author cyf
 * @date 2018-11-26 19:28:36
 */
@Controller
@RequestMapping("${sys.backendPath}/config")
public class ConfigController extends BaseController {

	private String PREFIX = "/backend/system/config/";

	@Autowired
	private IConfigService configService;
	

    /**
     * 跳转到参数配置表列表首页
     */
    @RequestMapping("")
    @Permission("config:list")
    public String index() {
        return PREFIX + "config.html";
    }
	
    /**
     * 跳转到新增参数配置表
     */
    @RequestMapping("/to_add")
    @Permission("config:add")
    public String toAdd() {
        return PREFIX + "config_add.html";
    }
	
	/**
     * 跳转到修改参数配置表
     */
    @RequestMapping("/to_update/{configId}")
    @Permission("config:update")
    public String toUpdate(@PathVariable Long configId, Model model) {
        Config config = this.configService.selectById(configId);
        model.addAttribute("config",new ConfigWarpper(config).warp());
        return PREFIX + "config_edit.html";
    }

    /**
     * 获取参数配置表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("config:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<Config> page = new PageFactory<Config>().defaultPage();
        List<Config> result = this.configService.page(page,params);
        page.setRecords((List<Config>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增参数配置表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("config:add")
    @BussinessLog(value = "新增参数配置表")
    @CacheClear(IConfigCache.CACHE_NAME)
    public Object add(Config config) {
        if (CommonUtil.isOneEmpty(config)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        // 判断有没有
        String v = ConfigCache.me().getValueByKey(config.getParamKey());
        if (CommonUtil.isNotEmpty(v)) {
            throw new BizException("参数key已存在");
        }
        config.insert();
        return R.ok();
    }

    /**
     * 删除参数配置表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("config:delete")
    @BussinessLog(value = "删除参数配置表")
    @CacheClear(IConfigCache.CACHE_NAME)
    public Object delete(@RequestParam Long configId) {
	
        this.configService.deleteById(configId);

        return R.ok();
    }

    /**
     * 修改参数配置表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("config:update")
    @BussinessLog(value = "修改参数配置表")
    @CacheClear(IConfigCache.CACHE_NAME)
    public Object update(Config config) {
        if (CommonUtil.isOneEmpty(config)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        Config old = this.configService.selectById(config.getId());

        old.setName(config.getName());
        old.setParamKey(config.getParamKey());
        old.setParamValue(config.getParamValue());
        old.setCategoryId(config.getCategoryId());
        old.setDescription(config.getDescription());
        old.setWeight(config.getWeight());
        old.updateById();
        return R.ok();
    }
	
}
